﻿using EquipViewAppNet;
using SecsEquip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace EquipSecs300
{
    public partial class SecsConfig : CMyFormEx
    {
        private SecsParameter secsParament = SecsParameterHelper.SecsParam;
        public SecsConfig()
        {
            InitializeComponent();
        }

       
        // 更新数据
        public override void UpdateData()
        {
            InitControlData();
            //RefreshView(secsParament);
        }

        void InitControlData()
        {
            textBoxMDLN.Text = secsParament.InterfaceName;
            textBoxDeviceID.Text = secsParament.DeviceID.ToString();

            textBoxIP.Text = secsParament.Ip.ToString();
            textBoxPort.Text = secsParament.Port.ToString();
            checkBoxPassive.Checked = secsParament.Passive;
            checkBoxEnableLog.Checked = secsParament.EnableLog;
            checkBoxComunication.Checked = secsParament.CommEnable;

            textBoxT1.Text = secsParament.T1.ToString();
            textBoxT2.Text = secsParament.T2.ToString();
            textBoxT3.Text = secsParament.T3.ToString();
            textBoxT4.Text = secsParament.T4.ToString();
            textBoxT5.Text = secsParament.T5.ToString();
            textBoxT6.Text = secsParament.T6.ToString();
            textBoxT7.Text = secsParament.T7.ToString();

            if (secsParament.ControlMode == CONTROL_MODE.eCONTROL_MODE_OFFLINE)
            {
                radioButtonOffline.Checked = true;
            }
            else if (secsParament.ControlMode == CONTROL_MODE.eCONTROL_MODE_ONLINE_LOCAL)
            {
                radioButtonLocal.Checked = true;
            }
            else if (secsParament.ControlMode == CONTROL_MODE.eCONTROL_MODE_ONLINE_REMOTE)
            {
                radioButtonRemote.Checked = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            SaveControlMode();
        }

        private void buttonSet_Click(object sender, EventArgs e)
        {
            int nTmp;

            //// checkBoxComunication, comboBoxControl 这俩个开软件时候设置

            secsParament.InterfaceName = textBoxMDLN.Text;

            nTmp = StringToNum.atoi(textBoxDeviceID.Text.ToString());
            m_pSecs.SetDeviceID(nTmp);
            secsParament.DeviceID = nTmp;

            secsParament.Ip = textBoxIP.Text;

            nTmp = StringToNum.atoi(textBoxPort.Text.ToString());
            secsParament.Port = nTmp;

            secsParament.Passive = checkBoxPassive.Checked;

            secsParament.EnableLog = checkBoxEnableLog.Checked;

            secsParament.CommEnable = checkBoxComunication.Checked;

            nTmp = StringToNum.atoi(textBoxT1.Text.ToString());
            secsParament.T1 = nTmp;
            nTmp = StringToNum.atoi(textBoxT2.Text.ToString());
            secsParament.T2 = nTmp;
            nTmp = StringToNum.atoi(textBoxT3.Text.ToString());
            secsParament.T3 = nTmp;
            nTmp = StringToNum.atoi(textBoxT4.Text.ToString());
            secsParament.T4 = nTmp;
            nTmp = StringToNum.atoi(textBoxT5.Text.ToString());
            secsParament.T5 = nTmp;
            nTmp = StringToNum.atoi(textBoxT6.Text.ToString());
            secsParament.T6 = nTmp;
            nTmp = StringToNum.atoi(textBoxT7.Text.ToString());
            secsParament.T7 = nTmp;

            if (radioButtonOffline.Checked)
            {
                secsParament.ControlMode = CONTROL_MODE.eCONTROL_MODE_OFFLINE;
            }
            else if (radioButtonLocal.Checked)
            {
                secsParament.ControlMode = CONTROL_MODE.eCONTROL_MODE_ONLINE_LOCAL;
            }
            else if (radioButtonRemote.Checked)
            {
                secsParament.ControlMode = CONTROL_MODE.eCONTROL_MODE_ONLINE_REMOTE;
            }

            secsEquipHelper.SetSecsParament(secsParament);
            SecsParameterHelper.SecsParemFunc.SaveSecsParament();
        }


        // 启用通讯
        private void checkBoxComunication_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxComunication.Checked)
            {
                m_pSecs.CommEnable();
            }
            else
            {
                m_pSecs.CommDisable();
            }

        }


        // 启用LOG
        private void checkBoxEnableLog_CheckedChanged(object sender, EventArgs e)
        {
            m_pSecs.SetEnableLog(checkBoxEnableLog.Checked);
        }

        private void checkBoxPassive_CheckedChanged(object sender, EventArgs e)
        {
            m_pSecs.SetPassive(secsParament.Passive);
        }

        private void radioButtonControl_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void buttonCostomizeHandlePP_Click(object sender, EventArgs e)
        {
            secsEquipHelper.PPCostomize.CostomizeHandlePP();
        }


        void LoadPortModeChange(LoadPortAccessMode loadPortAccessMode)
        {
            LoadPort loadPort = new LoadPort();
            loadPort.nAccessMode = loadPortAccessMode;

            loadPort.nPortID = 1;
            m_pSecs.CmsLoadPortCreate(loadPort);

            loadPort.nPortID = 2;
            m_pSecs.CmsLoadPortCreate(loadPort);

            loadPort.nPortID = 3;
            m_pSecs.CmsLoadPortCreate(loadPort);

            loadPort.nPortID = 4;
            m_pSecs.CmsLoadPortCreate(loadPort);
        }

        private void radioLoadPort_CheckedChanged(object sender, EventArgs e)
        {
            if (radioLoadPortAuto.Checked)
            {
                LoadPortModeChange(LoadPortAccessMode.AUTO);
            }
            else if (radioLoadPortManual.Checked)
            {
                LoadPortModeChange(LoadPortAccessMode.MANUAL);
            }
        }

        private bool remoteBuf;
        private void SaveControlMode()
        {
            if (remoteBuf != GlobalVar.EAPRemote)
            {
                remoteBuf = GlobalVar.EAPRemote;
                if (GlobalVar.EAPRemote)
                {
                    secsParament.ControlMode = CONTROL_MODE.eCONTROL_MODE_ONLINE_REMOTE;
                }
                else
                {
                    secsParament.ControlMode = CONTROL_MODE.eCONTROL_MODE_ONLINE_LOCAL;
                }

                //m_pSecs.SetControlMode(CONTROL_MODE.eCONTROL_MODE_OFFLINE);
                //Thread.Sleep(500);

                secsEquipHelper.SetSecsParament(secsParament);
                SecsParameterHelper.SecsParemFunc.SaveSecsParament();
            }
        }
       
    }
}
